<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="nav">
        <a href="#/page1" >page1</a>
        <a href="#/page2" >page2</a>
        <a href="#/page3" >page3</a>
    </div>
   
    <div id="container"></div>
    <script>
        // window.addEventListener('hashchange' ,function(event){
        //     let newURL = event.newURL;
        //     let oldURL = event.oldURL;
        //     console.log(newURL,oldURL);
        //     let route = newURL.split('#/')[1];
        // })
        class HashRouter{
            //事件监听
            //配置{}
            constructor(){
                this.hashURLobj = {};
                window.addEventListener('hashchange',(e)=>{
                    let newURL = e.newURL;
                    let hashURL = newURL.split('#')[1];
                    const callback = this.hashURLobj[hashURL];
                    if(typeof callback =='function'){
                        callback();
                    }
                })
                
            }
            register(hashUrl,fun){
                this.hashURLobj[hashUrl] = fun.bind(this);
            }

        }
        let router = new HashRouter()
        let container = document.getElementById('container');
        router.register('/page1',()=>container.innerHTML = 'i am page1');
        router.register('/page2',()=>container.innerHTML = 'i am page2');
        router.register('/page3',()=>container.innerHTML = 'i am page3');

    </script>
    
</body>
</html>